package com.qingxin.jcdw.action;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import com.qingxin.jcdw.entity.Jcdw;
import com.qingxin.jcdw.service.JcdwService;
import com.qingxin.sys.action.BaseAction;
import com.qingxin.sys.mvc.PageParam;
import com.qingxin.sys.mvc.Pager;
import com.qingxin.sys.mvc.SysError;

@Controller
@RequestMapping("/jcdw")
public class JcdwAction extends BaseAction {
	
	@Autowired
	private JcdwService jcdwService;
	
	@RequestMapping(value="/list.do", method=RequestMethod.GET)
	public String list(ModelMap model, HttpServletRequest request){
		return "trace/jcdw/jcdw_list";
	}
	
	/**
	 * 列表数据
	 * @param datatype
	 * @param search
	 * @param pageParam
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/listdata.json" , method={RequestMethod.GET, RequestMethod.POST})
	@ResponseBody
	public Object listdata(String datatype, String search, PageParam pageParam, HttpServletRequest request){
		Pager pager = jcdwService.list(pageParam,search, request);
		return pager;
	}
	
	/**
	 * 添加
	 * @param model
	 * @param jcdwid
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/add.do", method=RequestMethod.GET)
	public String add(ModelMap model,String jcdwid, HttpServletRequest request){
		model.put("jcdwid", jcdwid);
		return "/trace/jcdw/jcdw_add";
	}
	
	/**
	 * 保存添加
	 * @param jcdw
	 * @param result
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/save.json", method={RequestMethod.POST,RequestMethod.GET})
	@ResponseBody
	public Object save(@Valid Jcdw jcdw,BindingResult result, HttpServletRequest request){
		if(result.hasErrors()){
			return getSysErrors(result);
		}
		SysError info = jcdwService.add(jcdw,getLoginUser(request), request);
		return info;
	}
	
	/**
	 * 删除
	 * @param jcdwid
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/delete.json", method=RequestMethod.POST)
	@ResponseBody
	public Object delete(String[] jcdwid, HttpServletRequest request){
		if(jcdwid==null ){
			return new SysError("没有选择任何信息.");
		}
		return jcdwService.delete(jcdwid, request);
	}
	
	/**
	 * 修改
	 * @param model
	 * @param jcdwid
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/edit.do", method=RequestMethod.GET)
	public String edit(ModelMap model, String jcdwid, HttpServletRequest request){
		Jcdw jcdw = jcdwService.get(jcdwid);
		model.put("jcdw", jcdw);
		return "trace/jcdw/jcdw_edit";
	}
	
	/**
	 * 保存修改
	 * @param repertory
	 * @param result
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/update.json", method=RequestMethod.POST)
	@ResponseBody
	public Object update(@Valid Jcdw jcdw, BindingResult result, HttpServletRequest request){
		if(result.hasErrors()){
			return getSysErrors(result);
		}
		return jcdwService.update(jcdw, getLoginUser(request), request);
	}
	
	
	
	
}
